<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>用户登录</title>
  <link rel="shortcut icon" href="/static/imag/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" href="/static/css/login.css">
</head>

<body>
  <div class="Box">
    <div class="left">
      <div class="centerBox">
        <img src="/static/imag/logo.png" alt="" />
        <h4>多模态智能校园管理平台</h4>
        <p>用户登录</p>
      </div>
    </div>
    <div class="right">
      <form action="/login" method="post" id="sub">
        <h3>用户登录</h3>
        <input type="text" name="username" placeholder="请输入用户名" required />
        <input type="password" name="passwd" placeholder="请输入密码" required />
        <input type="submit" class="loginBtn" value="登录" />
        <p class="no">还没有账号？<a href="/register">去注册</a></p>
      </form>
    </div>
  </div>
</body>
<script text="javascript">
  var sub = document
    .getElementById("sub")
    .addEventListener("submit", function (event) {
      event.preventDefault();
      var formData = new FormData(this);
      fetch("/login", {
        method: "POST",
        body: formData,
      })
        .then((response) => response.json())
        .then((data) => {
          if (data.parmcode != 1) {
            console.log(data);
            alert("登录失败请重试，失败原因：" + data.msg);
            return;
          }
          console.log(data);
          var jwt = data.jwt;
          document.cookie = "" + jwt;
          console.log("ok")
          window.location.replace("/homepage")
        })
        .catch((error) => {
          // 处理错误情况
          alert("未知原因的错误，请重试");
          console.error("Error:", error);
        });
    });
</script>

</html>